#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid     = " xx";
const char* password = "xx ";

IPAddress local_IP(192, 168, 31, 101);
IPAddress gateway(192, 168, 31, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(210,34,0,14); //optional
IPAddress secondaryDNS(210,34,0,18); //optional

const int localPort = 3333;
WiFiUDP udp;
uint8_t verify[2]={0xff,0x01};
int packetSizeRcv;
char buf[4];

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,28800);
int i1000=0;
int hour;


//LED调光PWM
int ledcur1 = 255;
int ledset1 = 255;
int ledcur2 = 255;
int ledset2 = 255;

uint32_t now_ms = 0;

void setup() {
  pinMode(16, OUTPUT);
  digitalWrite(16, HIGH);
  analogWriteFreq(20000);
  analogWriteRange(65025);
  analogWrite(5, 255);
  analogWrite(4, 255);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  digitalWrite(16, LOW);
  timeClient.begin();
  
  Serial.println("");
  Serial.println("WiFi connected!");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("ESP Mac Address: ");
  Serial.println(WiFi.macAddress());
  Serial.print("Subnet Mask: ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway IP: ");
  Serial.println(WiFi.gatewayIP());
  Serial.print("DNS: ");
  Serial.println(WiFi.dnsIP());
  udp.begin(localPort);
  
}

void loop() {
    udpRcv();
    if (millis() - now_ms > 30)
    {
        now_ms = millis();
        per_003sec();
    }
}

